package com.wanxi.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.DeviceVO;
import com.wanxi.VO.NewsVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.DeviceService;
import com.wanxi.service.NewsService;
import com.wanxi.service.impl.DeviceServiceImpl;
import com.wanxi.service.impl.NewsServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 8:51
 */
//@WebServlet("/back/news/findAll")
public class NewsFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2、获取前端传递后端的值
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        String classIds = req.getParameter("classIds");
        String enable = req.getParameter("enable");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //3.封装VO数据
        NewsVO newsVO = new NewsVO();
        newsVO.setName(Tool.nullToStr(name));
        newsVO.setContent(Tool.nullToStr(content));
        newsVO.setNews_class_id(Tool.strToInt(classIds));
        newsVO.setEnable(Tool.strToInt(enable));
        newsVO.setPage(Tool.strToPage(page));
        newsVO.setLimit(Tool.strToLimit(limit));


        System.out.println("进入的了页面newsVO："+newsVO.toString());
               // 4.调用服务逻辑层
        NewsService newsService = new NewsServiceImpl();
        //        5.得到返回的结果
        ResultDTO resultDTO = newsService.findAll(newsVO);
        System.out.println("DeviceVO返回值："+newsVO.toString());
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
